﻿/******************************************************************************
 *  作者：       Maxzhang1985
 *  创建时间：   2012/4/18 11:23:55
 *
 *
 ******************************************************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.Composition;
using ExtendPropertyLib.WPF;
using ExtendPropertyLib;

using System.IO;
using ExORMDesginer.Services;

using ExORMDesginer.Models;
using ExORMDesginer.Helper;
using ExORMDesginer.Plugin;
using ExORMDesginer.Plugin.Template;

namespace ExORMDesginer.UserControls
{
    public class CodeGeneratorViewModel : TabViewModel<CodeGeneratorInfo>
    {
        public override void OnDoCreate(ExtendObject item, params object[] args)
        {
            base.OnDoCreate(item, args);
            SelectTemplateProperty.ValueChanged += new EventHandler<ExtendPropertyValueChangedArgs>(SelectTemplateProperty_ValueChanged);
           
        }

        void SelectTemplateProperty_ValueChanged(object sender, ExtendPropertyValueChangedArgs e)
        {
            if (SelectTemplate != null)
            {
                codeView.SetTemplateText(Model.LoadTemplate(SelectTemplate));
                
            }
           
        }

        
        public static ExtendProperty TemplateTabSelectionProperty = RegisterProperty<CodeGeneratorViewModel>(v => v.TemplateTabSelection);
        /// <summary>
        /// 切换到模板Tab
        /// </summary>
        public bool TemplateTabSelection { set { SetValue(TemplateTabSelectionProperty, value); } get { return (bool)GetValue(TemplateTabSelectionProperty); } }

        
        public static ExtendProperty PreviewTabSelectionProperty = RegisterProperty<CodeGeneratorViewModel>(v => v.PreviewTabSelection);
        /// <summary>
        /// 切换到
        /// </summary>
        public bool PreviewTabSelection { set { SetValue(PreviewTabSelectionProperty, value); } get { return (bool)GetValue(PreviewTabSelectionProperty); } }


        public static ExtendProperty SelectEntityProperty = RegisterProperty<CodeGeneratorViewModel>(v => v.SelectEntity);
        /// <summary>
        /// 被选择实体
        /// </summary>
        public DbEntity SelectEntity { set { SetValue(SelectEntityProperty, value); } get { return (DbEntity)GetValue(SelectEntityProperty); } }

        public static ExtendProperty SelectTemplateProperty = RegisterProperty<CodeGeneratorViewModel>(v => v.SelectTemplate);
        /// <summary>
        /// 被选择模板
        /// </summary>
        public TemplateInfo SelectTemplate { set { SetValue(SelectTemplateProperty, value); } get { return (TemplateInfo)GetValue(SelectTemplateProperty); } }

        
        public static ExtendProperty DisplayEntitiesProperty = RegisterProperty<CodeGeneratorViewModel>(v => v.DisplayEntities);
        /// <summary>
        /// 显示在列表中的数据实体
        /// </summary>
        public SerializableObservableCollection<DbEntity> DisplayEntities { set { SetValue(DisplayEntitiesProperty, value); } get { return (SerializableObservableCollection<DbEntity>)GetValue(DisplayEntitiesProperty); } }
    

        public void ClientClick()
        {
            var isn = ServiceManager.GetService<DatabaseMetaService>();

            //VelocityEngine velocity = new VelocityEngine();

            //ExtendedProperties props = new ExtendedProperties();
            //velocity.Init(props);
            //Template template = velocity.GetTemplate(@"codeTemplate.vm");
            //VelocityContext context = new VelocityContext();



            //var DbEntitys = isn.TableMetas.Where(t => t.EntityViewType == EntityType.Table || t.EntityViewType == EntityType.View);



            //StringBuilder sb = new StringBuilder();
            //sb.AppendLine("using System;");
            //sb.AppendLine("using MaxZhang.EasyEntities.Persistence.Mapping;");
            //sb.AppendLine("namespace EasyEntities.Models");
            //sb.AppendLine("{");
            //foreach (var Entity in DbEntitys)
            //{
            //    if (Entity.Name.StartsWith("UQTAB"))
            //        continue;
            //    context.Put("Entity", Entity);
            //    StringWriter writer = new StringWriter();
            //    template.Merge(context, writer);
            //    sb.AppendLine(writer.GetStringBuilder().ToString());

            //}
            //sb.AppendLine("}");

            //DocumentGeneratorService.SaveTxt(@"d:\sss.cs", sb.ToString());

        }


        private ICodeGeneratorView codeView { get {
            return this.View as ICodeGeneratorView;
        } }

        public override void OnLoad()
        {
            
            
            Model.OnLoad();
        }

      

        /// <summary>
        /// 生成代码
        /// </summary>
        public void Generator()
        {
            var entity = SelectEntity.Clone();
            string code =  Model.GeneratorCode(SelectTemplate,entity);
            codeView.SetPreviewText(code);
            PreviewTabSelection = true;
        }
           


    }
}
